if(!QS){
	var QS={};
}
QS.InterestRateLineChart=function(container,config){
	this.container=container;
	this.config={
		dataURL:'../../invoke/obtainLiborData',
		width:150,
		height:100,
		showX:false,
		showY:true,
		showYScale:false,
		monthGap:3,
		lineType:'line',
		tickerConfig:{
			'BBALibor1MonthGBP':{color:{line:'#FFAA00'}},
			'BBALibor12MonthGBP':{color:{line:'#0000FF'}}
		},
		legendColor:'#A0A0A0'
	};
	if(config){
		$.extend(this.config,config);
	}
	this.tabMenu=null;
	this.chartObj=null;
	this.el=null;
	this.tabMenuProfile=[{
		val:1,
		txt:'1 Month',
		closable:false
	},{
		val:12,
		txt:'1 Year',
		closable:false
	}];
	this.html='<div class="qs-interestrate clearfix"><div class="qs-interestrate-hd"></div><div class="qs-interestrate-YLabel"></div><div class="qs-interestrate-bd" style="position:relative;border:1px solid #76A3DA;"></div></div>';
	this.shortMonth=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	this.cacheData={};
	this.currentTabValue=1;
	this.canvasWidth=0;
	this.canvasHeight=0;
	this.init();
};

QS.InterestRateLineChart.prototype={
	init:function(){
		this.el=$(this.html).css({width:this.config.width,height:this.config.height}).appendTo(this.container);
		this.headEl=this.el.find('.qs-interestrate-hd');
		this.bodyEl=this.el.find('.qs-interestrate-bd');
		this.YLabelEl=this.el.find('.qs-interestrate-YLabel');
		this._initTabMenu();
		this.headHeight=this.headEl.outerHeight(true);
		this.YLabelWidth=this.YLabelEl.outerWidth(true);
		this._setDynamicSize();
		this._initChart(this.canvasWidth,this.canvasHeight);
	},
	changeOutSize:function(w,h){
		if(w>0&&h>0&&(this.config.width!=w||this.config.height!=h)){
			this.config.width=w;
			this.config.height=h;
			this.el.css({width:this.config.width,height:this.config.height});
			this._setDynamicSize();
			this.chartObj.changeOutSize(this.canvasWidth,this.canvasHeight);
			this._fillText(this.currentTabValue,this.cacheData[this.currentTabValue].map);
		}
	},
	_setDynamicSize:function(){
		this.YLabelEl.height(this.config.height-this.headHeight);
		this.canvasWidth=this.config.width-this.YLabelWidth-(this.bodyEl.outerWidth(true)-this.bodyEl.width());
		this.canvasHeight=this.config.height-this.headHeight-(this.bodyEl.outerHeight(true)-this.bodyEl.height());
	},
	_initTabMenu:function(){
		var self=this;
		this.tabMenu=new Util.TabMenu(this.headEl[0],this.tabMenuProfile,{
			onActive:function(tab){
				self.currentTabValue=tab.val;
				self._drawChart(tab.val);	
			},
			onDeActive:function(tab){
				
			}
		},{
			width:30,
			isExcuteCallbackAtInit:false,
			autoSlideToActiveTab:false
		});
	},
	_initChart:function(w,h){
		var _cfg = {
			canvasWidth:w,
			canvasHeight:h,
			showX:this.config.showX,
			showY:this.config.showY,
			showYScale:this.config.showYScale,
			lineType:this.config.lineType,
			mutiline:true,
			customLabel:true,
			customYLabelContainer:this.YLabelEl,
			align:'left',
			marginTop:this.headHeight,
			color:{axisColor:'#00000'}
		};
		this.chartObj=new chartCanvas(this.bodyEl,_cfg);
		this._drawChart(this.currentTabValue);
	},
	_initLegend:function(marginLeft){
		this.el.find('.qs-interestrate-legend').remove();
		var ticker='Libor',color={};
		for(var i in this.config.tickerConfig){
			if(i=='BBALibor1MonthGBP'){
				ticker='Libor';
				color['legend1M']=this.config.tickerConfig[i].color.line;
			}else if(i=='BBALibor12MonthGBP'){
				ticker='Libor';
				color['legend1Y']=this.config.tickerConfig[i].color.line;
			}else if(i=='Euribor1MonthEUR'){
				ticker='Euribor';
				color['legend1M']=this.config.tickerConfig[i].color.line;
			}else if(i=='Euribor12MonthEUR'){
				ticker='Euribor';
				color['legend1Y']=this.config.tickerConfig[i].color.line;
			}
		}
		var legendEl=$('<span class="qs-interestrate-legend"><span class="qs-interestrate-legend-label" style="color:'+this.config.legendColor+'">'+ticker+'&nbsp;</span><span class="legend1M" style="color:'+color['legend1M']+'">1M</span><font color="'+this.config.legendColor+'">/</font><span class="legend1Y" style="color:'+color['legend1Y']+'">1Y</span></span>').appendTo(this.el);
		var top=this.config.height-legendEl.outerHeight(true);
		legendEl.css({top:top,left:marginLeft+5});
	},
	_drawChart:function(tabValue){
		var self=this;
		if(self.cacheData[tabValue]==null){
			this._requestData(this._getPara(tabValue),function(o){
				if(o.errorCode!="200"){
					return;
				}
				self.cacheData[tabValue]=self._parseData(o.data);
				self.chartObj.drawMutiChart(self.cacheData[tabValue].list);
				self._fillText(tabValue,self.cacheData[tabValue].map);
			});
		}else{
			self.chartObj.drawMutiChart(self.cacheData[tabValue].list);
			self._fillText(tabValue,self.cacheData[tabValue].map);
		}
		//var o={"errorCode":0,data:[{"ticker":"BBALibor1MonthGBP","yield":[0.636,0.6335,0.631,0.6285,0.6235,0.62163,0.62038,0.6185,0.616,0.616,0.6135,0.61225,0.61163,0.60913,0.59975,0.59788,0.59475,0.59163,0.58913],"date":[41076,41077,41078,41079,41080,41083,41084,41085,41086,41087,41090,41091,41092,41093,41094,41097,41098,41099,41100]},{"ticker":"BBALibor12MonthGBP","yield":[1.73438,1.719,1.71213,1.70713,1.69963,1.69338,1.69088,1.68713,1.68338,1.68025,1.67525,1.66838,1.66275,1.6565,1.6465,1.64338,1.63713,1.62963,1.61588],"date":[41076,41077,41078,41079,41080,41083,41084,41085,41086,41087,41090,41091,41092,41093,41094,41097,41098,41099,41100]}]};
	},
	_parseData:function(o){
		var dMap={},dList=[];
		for(var i=0,l=o.length;i<l;i++){
			var d=o[i];
			var yieldArr=[],dateArr=[];
			if(this.config.tickerConfig[d.ticker]){
				for(var j=0,jl=d.yield.length;j<jl;j++){
					yieldArr.push([j,d.yield[j]]);
					dateArr.push([j,d.date[j]])
				}
				dMap[d.ticker]={yield:yieldArr,date:dateArr};
				dList.push({data:yieldArr,styleCfg:this.config.tickerConfig[d.ticker]});
			}
		}
		return {map:dMap,list:dList};
	},
	_getPara:function(range){
		var para={range:range};
		var ticker=[];
		for(var t in this.config.tickerConfig){
			ticker.push(t)
		}
		para['ticker']=ticker.join(',');
		return para;
	},
	_requestData:function(para,callback){
		$.get(this.config.dataURL,para,function(o){
			if(o!=''){
				o=eval('('+o+')');
				callback(o);
			}
		});
	},
	_fillText:function(range,map){
		if(this.chartObj){
			for(var t in map){
				data=map[t].date;
				break;
			}
			if(data.length==0)return;
			var axisx=this.chartObj.axis.x;
			var marginLeft=this.YLabelWidth;//axisx.margin;
			this._initLegend(marginLeft);
			var unqiueMonth=this._findUnqiueMonthh(data);
			var labelData=[];
			if(range==1){
				var last=unqiueMonth[unqiueMonth.length-1];
				var x=axisx.getCanvasPositionByPrice(last.index);
				labelData.push({left:x,date:last.date});
			}else if(range==12){
				for(var i=0,l=unqiueMonth.length;i<l;){
					var d=unqiueMonth[i];
					var x=axisx.getCanvasPositionByPrice(d.index);
					labelData.push({left:x,date:d.date});
					i+=this.config.monthGap;
				}
			}
			if(labelData.length>0){
				this._fillXLable(labelData);
			}
		}
	},
	
	_findUnqiueMonthh:function(data){
		var index=0,unqiueMonth=[],temp={};
		for(var i=0,l=data.length;i<l;i++){
			var d=this._getDateByRangeDays(data[i][1]);
			var year=d.getFullYear(),month=d.getMonth()+1,day=d.getDate();
			var key=year+(month<10?('0'+month):month);
			if(!temp[key]){
				temp[key]=true;
				unqiueMonth.push({index:i,date:[year,month,day]});
			}
		}
		return unqiueMonth;
	},
	
	_getDateByRangeDays:function(t){
		return new Date (t* 86400000+new Date(1900,0,1).getTime());
	},
	
	_fillXLable:function(data){
		this.bodyEl.find('.qs-interestrate-xline').remove();
		var html='';
		var l=data.length;
		for(var i=0;i<l;i++){
			if(i==0&&l>1){
				html+='<div class="qs-interestrate-xline noborder" style="left:'+data[i].left+'px"><em class="rotate month-label" style="color:'+this.config.legendColor+'">'+this._mappingLabel(data[i].date[1])+'</em></div>'
			}else{
				var style="";
				var emTxt=this._mappingLabel(data[i].date[1]);
				if(this.canvasWidth-data[i].left<14){
					style="left:-24px";
					var m=data[i].date[1]-1;
					m=m==0?12:m;
					emTxt=this._mappingLabel(m);
				}
				html+='<div class="qs-interestrate-xline" style="left:'+data[i].left+'px"><em class="rotate month-label" style="color:'+this.config.legendColor+';'+style+'">'+emTxt+'</em></div>'
			}
		}
		$(html).appendTo(this.bodyEl);
	},
	_mappingLabel:function(month){
		return this.shortMonth[month-1];
	}
};